package com.java110.mall.cart.bmo.index.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.mall.cart.bmo.index.IIndexBMO;
import com.java110.dto.shop.ShopVisitDto;
import com.java110.dto.storeOrder.StoreOrderDto;
import com.java110.dto.storeOrder.StoreOrderCartDto;
import com.java110.dto.storeOrder.StoreOrderCartReturnDto;
import com.java110.intf.cart.IStoreOrderCartInnerServiceSMO;
import com.java110.intf.cart.IStoreOrderCartReturnInnerServiceSMO;
import com.java110.intf.shop.IShopVisitInnerServiceSMO;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ClassName IndexBMOImpl
 * @Description TODO
 * @Author wuxw
 * @Date 2021/5/13 17:08
 * @Version 1.0
 * add by wuxw 2021/5/13
 **/
@Service("indexBMOImpl")
public class IndexBMOImpl implements IIndexBMO {

    @Autowired
    private IStoreOrderCartInnerServiceSMO storeOrderCartInnerServiceSMOImpl;

    @Autowired
    private IStoreOrderCartReturnInnerServiceSMO storeOrderCartReturnInnerServiceSMOImpl;

    @Autowired
    private IShopVisitInnerServiceSMO shopVisitInnerServiceSMO;


    @Override
    public ResponseEntity<String> getTodayData(JSONObject param) {
        //查询7天内访问记录
        ShopVisitDto shopVisitDto = new ShopVisitDto();
        shopVisitDto.setShopId(param.getString("shopId"));
        shopVisitDto.setEightDate("1");
        List<ShopVisitDto> queryShopVisits = shopVisitInnerServiceSMO.queryShopVisits(shopVisitDto);
        ArrayList<String> sevenDaysList = getPastDates(8);
        ArrayList<Integer> dayData = new ArrayList<>();
        Date date1 = new Date();
        String pattern = "yyyy-MM-dd";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        for (String date : sevenDaysList) {
            int i = 0;
            for (ShopVisitDto dto : queryShopVisits) {
                if (date.equals(simpleDateFormat.format(dto.getCreateTime()))) {
                    i = i + 1;
                }
            }
            dayData.add(i);
        }
        //查询店铺访问次数
        long shopVisitCount = 0;
        shopVisitDto = new ShopVisitDto();
        shopVisitDto.setShopId(param.getString("shopId"));
        shopVisitDto.setTodayDate("1");
        shopVisitCount = shopVisitInnerServiceSMO.queryShopVisitsCount(shopVisitDto);

        //待处理单
        long waitShopOrderCount = 0;
        StoreOrderCartDto storeOrderDto = new StoreOrderCartDto();
        storeOrderDto.setState(StoreOrderDto.STATE_FINISH_PAY);
        storeOrderDto.setShopId(param.getString("shopId"));
        storeOrderDto.setTodayDate("1");
        waitShopOrderCount = storeOrderCartInnerServiceSMOImpl.queryStoreOrderCartsCount(storeOrderDto);

        //退单
        long returnOrderCount = 0;
        StoreOrderCartReturnDto storeOrderCartReturnDto = new StoreOrderCartReturnDto();
        storeOrderCartReturnDto.setShopId(param.getString("shopId"));
        storeOrderCartReturnDto.setTodayDate("1");
        int i = storeOrderCartReturnInnerServiceSMOImpl.queryStoreOrderCartReturnsCount(storeOrderCartReturnDto);
        returnOrderCount = storeOrderCartReturnInnerServiceSMOImpl.queryStoreOrderCartReturnsCount(storeOrderCartReturnDto);

        //成交金额
        /**
         *     public static final String STATE_WAIT_SEND = "2002";//待发货
         *     public static final String STATE_SENDING = "3003";//待收货
         *     public static final String STATE_WAIT_REPAIR = "4004";//待评价
         *     public static final String STATE_REQ_RETURN = "5005";//申请退款
         */
        double todayMoney = 0;
        storeOrderDto = new StoreOrderCartDto();
        storeOrderDto.setShopId(param.getString("shopId"));
        //storeOrderDto.setStates(new String[]{StoreOrderCartDto.STATE_WAIT_SEND});
        storeOrderDto.setTodayDate("1");
        todayMoney = storeOrderCartInnerServiceSMOImpl.queryStoreOrderCartsAmount(storeOrderDto);


        JSONObject paramOut = new JSONObject();
        paramOut.put("shopVisitCount", shopVisitCount);
        paramOut.put("waitShopOrderCount", waitShopOrderCount);
        paramOut.put("returnOrderCount", returnOrderCount);
        paramOut.put("todayMoney", todayMoney);

        paramOut.put("sevenDay", sevenDaysList);
        paramOut.put("sevenDayDatas", dayData);

        return ResultVo.createResponseEntity(paramOut);
    }


    /**
     * 获取过去或者未来 任意天内的日期数组
     *
     * @param intervals intervals天内
     * @return 日期数组
     */
    public ArrayList<String> getPastDates(int intervals) {
        ArrayList<String> pastDaysList = new ArrayList<>();
        for (int i = 1; i < intervals; i++) {//1开始，不含当天
            pastDaysList.add(getPastDate(i));
        }
        return pastDaysList;
    }

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }


}
